define(["jquery", "easy-admin",'layhttp','popover'], function ($, ea) {
    console.log(layui.button);
var laytpl=layui.laytpl
var form=layui.form
  var  upload = layui.upload
    var http=layui.http;
  var table = layui.table
    var popover = layui.popover;
  var  layer = layui.layer
    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'order/index',
        add_url: 'order/add',
        add_old_url: 'order/addOld',
        edit_url: 'order/edit',
        delete_url: 'order/delete',
        export_url: 'order/export',
        modify_url: 'order/modify',
        import_url: 'order/import',
        import_tpl_url: '/upload/20231026/08263c586860687faf4289e4a80cf66f.png',

    };
var ops={
    url: ea.url("order/import"),
    elem: '.import-btn'
    , done: function (res, index, up) {
        console.log(res);
        if (res.code ==1) {

            ea.msg.success('导入成功', () => {
                table.reloadData(init.table_render_id);
                // setTimeout(()=>{
                // upload.render(ops)
                // },500)
            })

        } else {
            table.reloadData(init.table_render_id);

            if(res.data)
            layer.confirm(res.data, {icon: 0, title:res.msg});
            else
                ea.msg.error(res.msg);
        }
    }
    , accept: 'file' //允许上传的文件类型
    , size: 1024*20
    // ,exts:'xls|xlsx|csv'

};
    var Controller = {
        index: function () {

            var select_list = {};
            $.ajax(
                {
                    url: ea.url("order/dict"),
                    type: "POST",
                    contentType: "application/json",
                    dataType: 'json',
                    async:false,//取消异步请求
                    success: function(res) {
                        if(res.code==1){
                            select_list=res.data
                        }
                    }
                });

      var table_ins=      ea.table.render({
          init: init,
          showSearch: true,
          cellExpandedMode: 'tips',
          toolbar: ['refresh',

              [{
                  text: '添加',
                  url: init.add_url,
                  method: 'open',
                  auth: 'add',
                  class: 'layui-btn layui-btn-normal layui-btn-sm',
                  icon: 'fa fa-plus ',
                  extend: 'data-full="true"',
                  // extend: 'data-width="1124px" data-height="720px"',
              }],
              [{
                  text: '旧账号添加',
                  url: init.add_old_url,
                  method: 'open',
                  auth: 'add',
                  class: 'layui-btn layui-btn-normal layui-btn-sm layui-bg-gray',
                  icon: 'fa fa-plus ',
                  extend: 'data-full="true"',
                  // extend: 'data-width="1124px" data-height="720px"',
              }],
              [{
                  text: '导入',
                  url: init.import_url,
                  method: 'none',
                  auth: 'import',
                  class: 'layui-btn layui-btn-normal layui-bg-orange layui-btn-sm import-btn',
                  icon: 'fa fa-arrows',
                  extend: '',
              }],
              [{
                  text: '含图片导出',
                  url: init.export_url + '?is_img=1',
                  method: 'none',
                  auth: 'export',
                  class: 'layui-btn layui-btn-normal layui-btn-sm layui-bg-green easyadmin-export-btn',
                  icon: 'fa fa-file-image-o',
                  extend: 'data-table-export=' + init.table_render_id + ' data-url=' + init.export_url + '?is_img=1',
              }],
              'delete', 'export', [{
                  text: '导入模板',

                  method: 'none',
                  auth: 'add',
                  class: 'layui-btn layui-btn-normal layui-btn-sm  layui-bg-cyan import-tpl-btn',
                  icon: 'fa fa-download ',
                  extend: '',
              }],],
          cols: [[
              {type: 'checkbox'},
              // {field: 'id', title: 'id'},
              {field: 'merchant_status', search: 'select', selectList: {'0': '待反馈', '1': '已反馈'}, title: '处理状态', width: 100},
              {field: 'quality_problem', title: '问题类型', width: 120, search: 'select', selectList: select_list['quality_problem']},
              {
                  field: 'admin_id', fieldAlias: 'admin_id', title: '处理人', width: 100, search: 'select', selectList: select_list['admin_id'],
                  templet: function (d) {
                      if (d.admin_id) {
                          return select_list['admin_id'][d.admin_id]
                      }
                      return ''
                  }
              },
              {field: 'shop_name', title: '店铺名称', width: 100, search: 'select', selectList: select_list['shop_name']},
              {field: 'trade_no', title: '订单编号', width: 190},
              {field: 'pay_time', title: '支付时间', width: 120, search: 'range'},
              {field: 'logistics_no', title: '物流单号', width: 120},
              {field: 'goods_name', title: '商品名称', width: 140},
              {field: 'receivable', title: '订单总价', width: 100},
              {field: 'payment_method', title: '打款方式', width: 100, search: 'select', selectList: select_list['payment_method']},
              {field: 'store_indemnity', title: '商家赔付', width: 100, search: true},
              // {field: 'receiver_address', title: '收件人地址',width:120,search: false},
              {field: 'cs_remark', title: '客服备注', width: 100, search: false},
              {field: 'spec_no', title: '规格编码', width: 100, search: false},

              {field: 'registrant', title: '登记人', width: 100,},
              {field: 'create_time', title: '登记日期', width: 120, search: 'range'},


              {field: 'platform_id', title: '平台名称', width: 100, search: 'select', selectList: select_list['platform_id']},

              // {field: 'consign_time', title: '发货时间',width:120},
              // {field: 'to_deliver_time', title: '送货时间',width:120,search: false},


              // {field: 'order_remark', title: '订单备注',width:100,search: false},


              // {field: 'supplier_name', title: '供应商名称',width:100,search: 'select',selectList:select_list['supplier_name']},


              // {field: 'images', title: '图片', templet: ea.table.image,imageJoin:' '},
              // {field: 'supplier_indemnity', title: '供应商赔付'},
              {
                  width: 130, title: '操作', templet: ea.table.tool, fixed: 'right', operat: [
                      [{
                          text: '编辑',
                          url: init.edit_url,
                          method: 'open',
                          auth: 'edit',
                          class: 'layui-btn layui-btn-xs layui-btn-success',
                          extend: 'data-full="true"',
                      },],
                      'delete']
              },
          ]],
      });
            $('body').on('click', '.import-tpl-btn',  ()=> {
             window.location.href='importTpl'

            });

            upload.render(ops)
            ea.listen();
        },
        renderUpload(getTpl,view,goods_data) {
            laytpl(getTpl).render(goods_data, function (html) {
                view.innerHTML = html;
            });
        },
        renderGoods(getTpl,view,goods_data){
            laytpl(getTpl).render(goods_data, function(html){
                view.innerHTML = html;
            });
            // 监听表单是否为必填项
            ea.api.formRequired();

            // 监听表单提交事件
            // ea.api.formSubmit(preposeCallback, ok, no, ex);

            // 初始化图片显示以及监听上传事件
            // ea.api.upload();

            // 监听富文本初始化
            ea.api.editor();

            // 监听下拉选择生成
            ea.api.select();

            // 监听时间控件生成
            ea.api.date();

            // 初始化layui表单
            form.render();
        },
        addOld: function () {
            upload.render({
                elem: '#test10'
                ,url: ea.url('ajax/upload')
                ,done: function(res){
                    if (res.code === 1) {
                        var url = res.data.url;
                        console.log(url);
                        // $(elem).val(url);
                        // $(elem).trigger("input");
                        upload_data.push(url)
                        Controller.renderUpload(uploadTpl,upload_view,upload_data)
                        ea.msg.success(res.msg);
                    } else {
                        ea.msg.error(res.msg);
                    }
                }
            });

            popover.create('.pay-image-popover',
                {title:'',
                    content:'转账或者打款的截图',
                    animation:'pop',
                    closeable:false,
                    placement:'right',
                    trigger:'hover',
                    delay: {
                        //show and hide delay time of the popover, works only when trigger is 'hover',the value can be number or object
                        show: null,
                        hide: 100
                    },
                    opacity:0.98,
                    type:'html',//content type, values:'html','iframe','async'
                })
            var goods_data = {"list":[]}
            var upload_data = []
            // let upload_tpl=$('#upload_tpl')

            var uploadTpl = $('#upload_tpl').html()
                ,upload_view = document.getElementById('upload_view');

            console.log(uploadTpl);
            $('input[name="trade_no"]').focus(function(){
                // 在这里编写获得焦点的监听代码
            }).blur(function(){
                // 在这里编写失去焦点的监听代码
                let trade_no=$(this).val()
                if(trade_no){
                    var index = ea.msg.loading();

                    http.post({
                        url:'admin/order/queryOld',
                        data:{trade_no}
                    }).then(res=>{
                        ea.msg.close(index);
                        console.log(res);
                        $('input[name="logistics_no"]').val(res.logistics_no)
                        $('input[name="pay_time"]').val(res.pay_time)

                        $('input[name="receivable"]').val(res.receivable)
                        $('input[name="shop_name"]').val(res.shop_name)
                        $('input[name="to_deliver_time"]').val(res.to_deliver_time)
                        $('input[name="cs_remark"]').val(res.cs_remark)
                        $('#platform_name_input').val(res.platform_name)
                        $('input[name="platform_id"]').val(res.platform_id)
                        goods_data.list=res.goods_list
                        Controller.renderGoods(getTpl,view,goods_data)
                    },er=>{
                        ea.msg.close(index);
                        ea.msg.error(er)
                    })
                }
            });


            $('body').on('click', '.upload-img-item-del-btn',  function () {
                let index=$(this).attr('data-index')
                console.log(index);
                upload_data.splice(index,1)

                Controller.renderUpload(uploadTpl,upload_view,upload_data)
            });
            $('body').on('click', '.btn-del',  function () {
                let index=$(this).data('index')
                console.log(index);
                goods_data.list.splice(index,1)

                Controller.renderGoods(getTpl,view,goods_data)
            });
            $('body').on('click', '.btn-add',  ()=> {
                goods_data.list.push({})
                this.renderGoods(getTpl,view,goods_data)
            });


            let goods_tpl=$('#goods_tpl')

            var getTpl = goods_tpl.html()
                ,view = document.getElementById('view');

            ea.listen(function (data) {
                console.log(data);
                data.goods_name=data['goods[0][goods_name]']
                data.spec_no=data['goods[0][spec_no]']
                data.quality_problem=data['goods[0][quality_problem]']
                console.log(data);
                data.images=upload_data.join('|')
                return data
            });
        },
        add: function () {
            upload.render({
                elem: '#test10'
                ,url: ea.url('ajax/upload')
                ,done: function(res){
                    if (res.code === 1) {
                        var url = res.data.url;
                       console.log(url);
                        // $(elem).val(url);
                        // $(elem).trigger("input");
                        upload_data.push(url)
                        Controller.renderUpload(uploadTpl,upload_view,upload_data)
                        ea.msg.success(res.msg);
                    } else {
                        ea.msg.error(res.msg);
                    }
                }
            });

            popover.create('.pay-image-popover',
                {title:'',
                    content:'转账或者打款的截图',
                animation:'pop',
                closeable:false,
                placement:'right',
                trigger:'hover',
                delay: {
                    //show and hide delay time of the popover, works only when trigger is 'hover',the value can be number or object
                    show: null,
                    hide: 100
                },
                opacity:0.98,
                type:'html',//content type, values:'html','iframe','async'
            })
            var goods_data = {"list":[]}
            var upload_data = []
            // let upload_tpl=$('#upload_tpl')

            var uploadTpl = $('#upload_tpl').html()
                ,upload_view = document.getElementById('upload_view');

console.log(uploadTpl);
            $('input[name="trade_no"]').focus(function(){
                // 在这里编写获得焦点的监听代码
            }).blur(function(){
                // 在这里编写失去焦点的监听代码
                let trade_no=$(this).val()
                if(trade_no){
                var index = ea.msg.loading();

                http.post({
                    url:'admin/order/query',
                    data:{trade_no}
                }).then(res=>{
                    ea.msg.close(index);
                    console.log(res);
                    $('input[name="logistics_no"]').val(res.logistics_no)
                    $('input[name="pay_time"]').val(res.pay_time)

                    $('input[name="receivable"]').val(res.receivable)
                    $('input[name="shop_name"]').val(res.shop_name)
                    $('input[name="to_deliver_time"]').val(res.to_deliver_time)
                    $('input[name="cs_remark"]').val(res.cs_remark)
                    $('#platform_name_input').val(res.platform_name)
                    $('input[name="platform_id"]').val(res.platform_id)
                    goods_data.list=res.goods_list
                    Controller.renderGoods(getTpl,view,goods_data)
                },er=>{
                    ea.msg.close(index);
                    ea.msg.error(er)
                })
                }
            });
            $('input[name="logistics_no"]').focus(function(){
                // 在这里编写获得焦点的监听代码
            }).blur(function(){
                // 在这里编写失去焦点的监听代码
                if($('input[name="trade_no"]').val()){
                    return
                }
                let logistics_no=$(this).val()
                if(logistics_no){
                    var index = ea.msg.loading();

                    http.post({
                        url:'admin/order/query',
                        data:{logistics_no}
                    }).then(res=>{
                        ea.msg.close(index);
                        console.log(res);
                        $('input[name="trade_no"]').val(res.trade_no)
                        $('input[name="logistics_no"]').val(res.logistics_no)
                        $('input[name="pay_time"]').val(res.pay_time)

                        $('input[name="receivable"]').val(res.receivable)
                        $('input[name="shop_name"]').val(res.shop_name)
                        $('input[name="to_deliver_time"]').val(res.to_deliver_time)
                        $('input[name="cs_remark"]').val(res.cs_remark)
                        $('#platform_name_input').val(res.platform_name)
                        $('input[name="platform_id"]').val(res.platform_id)
                        goods_data.list=res.goods_list
                        Controller.renderGoods(getTpl,view,goods_data)
                    },er=>{
                        ea.msg.close(index);
                        ea.msg.error(er)
                    })
                }
            });

            $('body').on('click', '.upload-img-item-del-btn',  function () {
                let index=$(this).attr('data-index')
                console.log(index);
                upload_data.splice(index,1)

                Controller.renderUpload(uploadTpl,upload_view,upload_data)
            });
            $('body').on('click', '.btn-del',  function () {
                let index=$(this).data('index')
                console.log(index);
                goods_data.list.splice(index,1)

                Controller.renderGoods(getTpl,view,goods_data)
            });
            $('body').on('click', '.btn-add',  ()=> {
                goods_data.list.push({})
                this.renderGoods(getTpl,view,goods_data)
            });


           let goods_tpl=$('#goods_tpl')

            var getTpl = goods_tpl.html()
                ,view = document.getElementById('view');

            ea.listen(function (data) {
                console.log(data);
                data.goods_name=data['goods[0][goods_name]']
                data.spec_no=data['goods[0][spec_no]']
                data.quality_problem=data['goods[0][quality_problem]']
                console.log(data);
data.images=upload_data.join('|')
return data
            });
        },
        edit: function () {
            upload.render({
                elem: '#test10'
                ,url: ea.url('ajax/upload')
                ,done: function(res){
                    if (res.code === 1) {
                        var url = res.data.url;
                        console.log(url);
                        // $(elem).val(url);
                        // $(elem).trigger("input");
                        upload_data.push(url)
                        Controller.renderUpload(uploadTpl,upload_view,upload_data)
                        ea.msg.success(res.msg);
                    } else {
                        ea.msg.error(res.msg);
                    }
                }
            });
            // let upload_tpl=$('#upload_tpl')

            var uploadTpl = $('#upload_tpl').html()
                ,upload_view = document.getElementById('upload_view');

            let order_id=$('input[name="id"]').val()
            let input_images=$('#input_images').val()
          var  upload_data=[]
            if(input_images){
                upload_data=input_images.split('|')
                Controller.renderUpload(uploadTpl,upload_view,upload_data)
            }
            $('body').on('click', '.upload-img-item-del-btn',  function () {
                let index=$(this).attr('data-index')
                console.log(index);
                upload_data.splice(index,1)

                Controller.renderUpload(uploadTpl,upload_view,upload_data)
            });
            var goods_data = { //数据

                "list":[
                ]
            }
            http.post({
                url:'admin/order/goods',
                data:{order_id}
            }).then(res=>{
                console.log(res);

                goods_data.list=res
                Controller.renderGoods(getTpl,view,goods_data)
            })
            $('body').on('click', '.btn-del',  ()=> {
                let index=$(this).data('index')
                console.log(index);
                goods_data.list.splice(index,1)

                this.renderGoods(getTpl,view,goods_data)
            });
            $('body').on('click', '.btn-add',  ()=> {
                goods_data.list.push({order_id})
                this.renderGoods(getTpl,view,goods_data)
            });


            let goods_tpl=$('#goods_tpl')

            var getTpl = goods_tpl.html()
                ,view = document.getElementById('view');

            ea.listen(function (data) {
                console.log(data);
                data.goods_name=data['goods[0][goods_name]']
                data.spec_no=data['goods[0][spec_no]']
                data.quality_problem=data['goods[0][quality_problem]']
                console.log(data);
                data.images=upload_data.join('|')
                return data
            });
        },
    };
    return Controller;
});
